/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package yagui4ctte;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSplitPane;

/**
 *
 * @author cordiehu
 */
public class Gui extends JPanel implements ActionListener, MouseListener{

    private JFrame frame;


    //Barre de menu
    private JMenuBar menu = new JMenuBar();
    private JMenu  file = new JMenu ("Fichier");
        private JMenuItem newI = new JMenuItem("Nouveau");
        private JMenuItem openI = new JMenuItem("Ouvrir");
        private JMenuItem openXMLI = new JMenuItem("Ouvrir un XML");
        private JMenuItem saveI = new JMenuItem("Enregistrer");
        private JMenuItem saveaI = new JMenuItem("Enregistrer-sous");
        private JMenu exportM = new JMenu("Exporter");
            private JMenuItem saveSTaI = new JMenuItem("Enregistrer le sous-arbre");
            private JMenuItem saveaXMLI = new JMenuItem("Enregistrer en XML");
            private JMenuItem saveaJPGI = new JMenuItem("Enregistrer en JPG");
            private JMenuItem saveSTaJPGI = new JMenuItem("Enregistrer le sous-arbre en JPG");
        private JMenuItem printaI = new JMenuItem("Imprimer...");
        private JMenuItem printI = new JMenuItem("Impression rapide");
        private JMenuItem quitI = new JMenuItem("Quitter");

    private JMenu  edit = new JMenu ("Editer");
        private JMenuItem undoI = new JMenuItem("Annuler");
        private JMenuItem redoI = new JMenuItem("Refaire");
        private JMenuItem cutSelI = new JMenuItem("Couper la selection");
        private JMenuItem cutSTI = new JMenuItem("Couper le sous-arbre");
        private JMenuItem copySelI = new JMenuItem("Copier la selection");
        private JMenuItem copySTI = new JMenuItem("Copier le sous-arbre");
        private JMenuItem pasteI = new JMenuItem("Coller");
        private JMenuItem deleteI = new JMenuItem("Supprimer");
        private JMenuItem findI = new JMenuItem("Chercher...");
        
    private JMenu  view = new JMenu ("Affichage");
        private JMenuItem taskPI = new JMenuItem("Propriétés de la tâche");
        private JMenuItem prioTI = new JMenuItem("Arbre de priorité");
        private JMenu byLvlI = new JMenu("Filtrer par niveau");
            private JMenuItem allLvl = new JMenuItem("Tous les niveaux");
        private JCheckBoxMenuItem cbMenuItem = new JCheckBoxMenuItem("Mode coopératif");
        private JMenuItem funfoldI = new JMenuItem("Develloper/Dédé-veut-lopez le sous-arbre");
        private JMenuItem unfoldAI = new JMenuItem("Tout réduire");

    private JMenu  info = new JMenu ("Infos");
        private JMenuItem taskModelStatI = new JMenuItem("Statistiques du modèle de tâches");
    private JMenu  insert = new JMenu ("Insertion");
        private ButtonGroup groupInsertBG = new ButtonGroup();
            private JRadioButton belowRB = new JRadioButton("En dessous (Enfant)");
            private JRadioButton siblingRB = new JRadioButton("Vers la gauche (Frère et Soeur)");
        private JMenuItem subtreeFromFileI = new JMenuItem("Un sous-arbre depuis un fichier");
        private JMenuItem newLevelI = new JMenuItem("Un nouveau niveau");
    private JMenu  tools = new JMenu ("Outils");
        private JMenuItem modelFilterI = new JMenuItem("Filtre de Modèle");
        private JMenuItem informalToFormalI = new JMenuItem("De l'informel vers le formel");
        private JMenuItem checkModelStructI = new JMenuItem("Verifier la structure du modèle");
        private JMenuItem startTaskModelSimulatorI = new JMenuItem("Lancer la simulation du modèle de tâche");
        private JMenuItem reachabilityAnalysisI = new JMenuItem("Analyse d'atteinte");
    
    //JPanels principaux
    private JPanel nordJP = new JPanel(new BorderLayout());
    private JPanel outilsJP = new JPanel(new BorderLayout());
    private JSplitPane workspaceJP = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    private JPanel statusBar = new JPanel();

    //sous-JPanels
        //sous-nordJP
    private JPanel raccourcisSJP = new JPanel(new FlowLayout());
        private JButton newB = new JButton(new ImageIcon("src/yagui4ctte/new.png"));
        private JButton openB = new JButton(new ImageIcon("src/yagui4ctte/ouvrir.png" ));
        private JButton cutB = new JButton(new ImageIcon("src/yagui4ctte/couper.png" ));
        private JButton pasteB = new JButton(new ImageIcon("src/yagui4ctte/coller.png" ));
        private JButton saveB = new JButton(new ImageIcon("src/yagui4ctte/save.png" ));
        private JButton undoB = new JButton(new ImageIcon("src/yagui4ctte/undo.png" ));
        private JButton redoB = new JButton(new ImageIcon("src/yagui4ctte/do.png" ));
    private JSplitPane taskOverviewJSP = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        //sous-workspaceJP
    private JPanel cooperative = new JPanel();
    private JPanel workspaceSJP = new JPanel();
        //sous-statusBar
    private JLabel statusLbl = new JLabel("Bienvenue dans Yagui4CTTE");

        //selecteur de fichier
    private JFileChooser selecteurJFC = new JFileChooser();




    public Gui(JFrame jf){
        super(new BorderLayout());
        this.frame = jf;
        //Menu
        this.menu.add(file);
            this.file.add(this.newI);
            this.file.addSeparator();
            this.file.add(this.openI);
            this.file.add(this.openXMLI);
            this.file.addSeparator();
            this.file.add(this.saveI);
            this.file.add(this.saveaI);
            this.file.add(this.exportM);
                this.exportM.add(this.saveSTaI);
                this.exportM.add(this.saveaXMLI);
                this.exportM.add(this.saveaJPGI);
                this.exportM.add(this.saveSTaJPGI);
            this.file.addSeparator();
            this.file.add(this.printaI);
            this.file.add(this.printI);
            this.file.add(this.quitI);
            
        this.menu.add(edit);
            this.edit.add(this.undoI);
            this.edit.add(this.redoI);
            this.edit.addSeparator();
            this.edit.add(this.cutSelI);
            this.edit.add(this.cutSTI);
            this.edit.add(this.copySelI);
            this.edit.add(this.copySTI);
            this.edit.add(this.pasteI);
            this.edit.add(this.deleteI);
            this.edit.addSeparator();
            this.edit.add(this.findI);

        this.menu.add(view);
            this.view.add(this.taskPI);
            this.view.add(this.prioTI);
            this.view.add(this.byLvlI);
                this.byLvlI.add(this.allLvl);
                this.setLevelFilterMenu();
            this.view.add(this.cbMenuItem);
            this.view.add(this.funfoldI);
            this.view.add(this.unfoldAI);

        this.menu.add(info);
            this.info.add(this.taskModelStatI);

        this.menu.add(insert);
            this.insert.add(this.belowRB);
            this.insert.add(this.siblingRB);
                this.groupInsertBG.add(this.belowRB);
                this.groupInsertBG.add(this.siblingRB);
                this.belowRB.setSelected(true);
            this.insert.addSeparator();
            this.insert.add(this.subtreeFromFileI);
            this.insert.add(this.newLevelI);

        this.menu.add(tools);
            this.tools.add(this.modelFilterI);
            this.tools.add(this.informalToFormalI);
            this.tools.add(this.checkModelStructI);
            this.tools.add(this.startTaskModelSimulatorI);
            this.tools.add(this.reachabilityAnalysisI);

        frame.setJMenuBar(this.menu);

        //Ajout de listeners
        this.newI.addActionListener(this);
        this.openI.addActionListener(this);
        this.openXMLI.addActionListener(this);
        this.saveI.addActionListener(this);
        this.saveaI.addActionListener(this);
        this.saveSTaI.addActionListener(this);
        this.saveaXMLI.addActionListener(this);
        this.saveaJPGI.addActionListener(this);
        this.saveSTaJPGI.addActionListener(this);
        this.printI.addActionListener(this);
        this.printaI.addActionListener(this);
        this.quitI.addActionListener(this);
        this.undoI.addActionListener(this);
        this.redoI.addActionListener(this);
        this.cutSelI.addActionListener(this);
        this.cutSTI.addActionListener(this);
        this.copySelI.addActionListener(this);
        this.copySTI.addActionListener(this);
        this.pasteI.addActionListener(this);
        this.deleteI.addActionListener(this);
        this.findI.addActionListener(this);
        this.taskPI.addActionListener(this);
        this.prioTI.addActionListener(this);
        this.allLvl.addActionListener(this);
        this.cbMenuItem.addActionListener(this);
        this.unfoldAI.addActionListener(this);
        this.funfoldI.addActionListener(this);
        this.taskModelStatI.addActionListener(this);
        this.belowRB.addActionListener(this);
        this.siblingRB.addActionListener(this);
        this.subtreeFromFileI.addActionListener(this);
        this.newLevelI.addActionListener(this);
        this.modelFilterI.addActionListener(this);
        this.informalToFormalI.addActionListener(this);
        this.checkModelStructI.addActionListener(this);
        this.startTaskModelSimulatorI.addActionListener(this);
        this.reachabilityAnalysisI.addActionListener(this);
        this.newB.addActionListener(this);
        this.openB.addActionListener(this);
        this.cutB.addActionListener(this);
        this.pasteB.addActionListener(this);
        this.undoB.addActionListener(this);
        this.redoB.addActionListener(this);
        this.saveB.addActionListener(this);
        this.newB.addMouseListener(this);
        this.openB.addMouseListener(this);
        this.cutB.addMouseListener(this);
        this.pasteB.addMouseListener(this);
        this.undoB.addMouseListener(this);
        this.redoB.addMouseListener(this);
        this.saveB.addMouseListener(this);
        this.newB.setToolTipText("Créer un nouvel arbre");
        this.openB.setToolTipText("Ouvrir un arbre existant");
        this.cutB.setToolTipText("Couper la selection");
        this.pasteB.setToolTipText("Coller la selection");
        this.undoB.setToolTipText("Défaire");
        this.redoB.setToolTipText("Refaire");
        this.saveB.setToolTipText("Enregistrer l'arbre");
        
        //Gestion de panels
        this.add(this.nordJP, BorderLayout.NORTH);
        this.add(this.outilsJP, BorderLayout.WEST);
        this.add(this.workspaceJP, BorderLayout.CENTER);
        this.add(this.statusBar, BorderLayout.SOUTH);

        this.raccourcisSJP.add(this.newB);
        this.raccourcisSJP.add(this.openB);
        this.raccourcisSJP.add(this.saveB);
        this.raccourcisSJP.add(this.undoB);
        this.raccourcisSJP.add(this.redoB);
        this.raccourcisSJP.add(this.cutB);
        this.raccourcisSJP.add(this.pasteB);
        this.raccourcisSJP.revalidate();

        this.nordJP.add(this.raccourcisSJP, BorderLayout.NORTH);
        this.nordJP.add(this.taskOverviewJSP, BorderLayout.CENTER);
        this.workspaceJP.add(this.workspaceSJP);
        /*this.workspaceJP.add(this.cooperative);*/
        this.workspaceSJP.revalidate();
        
        this.statusBar.add(this.statusLbl);

    }


    public void setStatusLabel(String s){
        this.statusLbl.setText(s);
    }

    private void setLevelFilterMenu() {
        //throw new UnsupportedOperationException("Not yet implemented");
    }

    public void actionPerformed(ActionEvent e) {
        // Pour le menu Fichier
       if (e.getSource().equals(this.newI)|| e.getSource().equals(this.newB)){
           this.workspaceJP.remove(this.workspaceSJP);
           this.workspaceSJP = new Arbre(this);
           this.workspaceJP.add(this.workspaceSJP);
           this.workspaceSJP.revalidate();
           this.revalidate();
       }
       if (e.getSource().equals(this.openI)|| e.getSource().equals(this.openB)){

            this.selecteurJFC.showOpenDialog(this);
            try{
                this.workspaceJP.remove(this.workspaceSJP);
                FileInputStream fis = new FileInputStream(this.selecteurJFC.getSelectedFile());
                ObjectInputStream in =  new ObjectInputStream(fis);
                this.workspaceSJP = (Arbre) in.readObject();
                this.workspaceJP.add(this.workspaceSJP);
                this.workspaceSJP.revalidate();
                this.revalidate();

                
            }catch(Exception ex){}
       }
       if (e.getSource().equals(this.openXMLI)){
             this.selecteurJFC.showOpenDialog(this);
             try{
                if (this.selecteurJFC.getSelectedFile().getName().contains(".xml")){
                    this.workspaceJP.remove(this.workspaceSJP);
                    FileInputStream fis = new FileInputStream(this.selecteurJFC.getSelectedFile());
                    ObjectInputStream in =  new ObjectInputStream(fis);
                    this.workspaceSJP = (Arbre) in.readObject();
                    this.workspaceJP.add(this.workspaceSJP);
                    this.workspaceSJP.revalidate();
                    this.revalidate();
                }
                else JOptionPane.showMessageDialog((Frame) this.frame,
                "Vous devez choisir un fichier XML");
            }catch(Exception ex){}
       }
       if (e.getSource().equals(this.saveI)|| e.getSource().equals(this.saveB)){

       }
       if (e.getSource().equals(this.saveaI)){
            this.selecteurJFC.showSaveDialog(this);
            try{
                FileOutputStream fis = new FileOutputStream(this.selecteurJFC.getSelectedFile());
                ObjectOutputStream out =  new ObjectOutputStream(fis);
                out.writeObject(this.workspaceSJP);
            }catch(Exception ex){}
       }
       if (e.getSource().equals(this.saveSTaI)){

       }
       if (e.getSource().equals(this.saveaXMLI)){

       }
       if (e.getSource().equals(this.saveaJPGI)){

       }
       if (e.getSource().equals(this.saveSTaJPGI)){

       }
       if (e.getSource().equals(this.printI)){
           
       }
       if (e.getSource().equals(this.printaI)){

       }
       if (e.getSource().equals(this.quitI)){
           System.exit(0);
       }


       // pour le menu Edition
       if (e.getSource().equals(this.undoI)){

       }
       if (e.getSource().equals(this.redoI)){

       }
       if (e.getSource().equals(this.cutSelI)){

       }
       if (e.getSource().equals(this.cutSTI)){

       }
       if (e.getSource().equals(this.copySelI)){

       }
       if (e.getSource().equals(this.copySTI)){

       }
       if (e.getSource().equals(this.pasteI)){

       }
       if (e.getSource().equals(this.deleteI)){

       }
       if (e.getSource().equals(this.findI)){

       }

       //pour le menu affichage
       if (e.getSource().equals(this.taskPI)){

       }
       if (e.getSource().equals(this.prioTI)){

       }
       if (e.getSource().equals(this.allLvl)){

       }
       if (e.getSource().equals(this.cbMenuItem)){
           if (this.cbMenuItem.getState())
                this.workspaceJP.add(this.cooperative);
           else this.workspaceJP.remove(this.cooperative);

       }
       if (e.getSource().equals(this.funfoldI)){

       }
       if (e.getSource().equals(this.unfoldAI)){

       }

       //pour le menu infos
       if (e.getSource().equals(this.taskModelStatI)){

       }

       //pour le menu insertion
       if (e.getSource().equals(this.belowRB)){

       }
       if (e.getSource().equals(this.siblingRB)){

       }
       if (e.getSource().equals(this.subtreeFromFileI)){

       }
       if (e.getSource().equals(this.newLevelI)){

       }

       //pour le menu outils
       if (e.getSource().equals(this.modelFilterI)){

       }
       if (e.getSource().equals(this.informalToFormalI)){

       }
       if (e.getSource().equals(this.checkModelStructI)){

       }
       if (e.getSource().equals(this.startTaskModelSimulatorI)){

       }
       if (e.getSource().equals(this.reachabilityAnalysisI)){

       }
    }

    public void mouseClicked(MouseEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseReleased(MouseEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        if(e.getSource() instanceof JButton){this.setStatusLabel(((JButton)e.getSource()).getToolTipText());}
    }

    public void mouseExited(MouseEvent arg0) {
        this.setStatusLabel("Bienvenue dans Yagui4CTTE");
    }

}